import {
  Get,
  Controller,
  UseInterceptors,
  Query,
  // Body,
  Req,
  Post,
} from '@nestjs/common';
import { LoggingInterceptor } from '../interceptor/logging.interceptor';
import { ElasticService } from '../service/elasticsearch.service';
import { Request } from 'express';
/**
 * ElasticController控制层
 *
 * @export
 * @class ElasticController
 */
@Controller('api/v1') // 添加url前缀
@UseInterceptors(LoggingInterceptor) // 使用LoggingInterceptor
export class ElasticController {
  // 依赖注入elasticService
  constructor(private readonly elasticService: ElasticService) {}

  // url对应 api/v1/lastest
  @Get('/lastest')
  async lastest(@Query('from') from: number) {
    let data;
    await this.elasticService
      .getLastestByPage(from)
      .then(res => {
        data = res.data;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/hotest
  @Get('/hotest')
  async hotest(@Query('from') from: number) {
    let data;
    await this.elasticService
      .getHotestByPage(from)
      .then(res => {
        data = res.data;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/coursels
  @Get('/coursels')
  async coursels() {
    let data;
    await this.elasticService
      .getCoursels()
      .then(res => {
        data = res.data;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/category
  @Get('/category')
  async category(
    @Query('type') type: string,
    @Query('sort') sort?: string,
    @Query('from') from?: number,
  ) {
    let data;
    await this.elasticService
      .getByCategory(type, sort, from)
      .then(res => {
        data = res;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/search
  @Get('/search')
  async searchByKeyword(@Query('keyword') keyword: string) {
    let data;
    await this.elasticService
      .searchByKeyword(keyword)
      .then(res => {
        data = res;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/searchDSL
  @Get('/searchDSL')
  async specialSearch(@Query('query') query: string) {
    let data;
    await this.elasticService
      .specialSearch(query)
      .then(res => {
        data = res;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/proxyQuery
  @Post('/proxyQuery')
  async proxyQuery(@Req() req: Request) {
    let data;
    await this.elasticService
      .proxyQuery(req.body)
      .then(res => {
        data = res.data;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/wordAnalyze
  @Get('/wordAnalyze')
  async wordAnalyze(@Query('text') text: string) {
    let data;
    await this.elasticService
      .wordAnalyze(text)
      .then(res => {
        data = res.data;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }

  // url对应 api/v1/
  @Get()
  async getOneById(@Query('id') id: string) {
    let data;
    await this.elasticService
      .getOneById(id)
      .then(res => {
        data = res;
      })
      .catch(err => {
        data = err;
      });
    return data;
  }
}
